<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/element-ui/index.css" />
    <link rel="stylesheet" href="../styles/common.css">
    <link rel="stylesheet" href="../styles/login.css">
    <link rel="stylesheet" href="../styles/icon/iconfont.css" />
    <style>
        .body{
            min-width: 1366px;
        }
    </style>
</head>

<body>
<div class="login" id="login-app">
    <div class="login-box">
        <img src="../img/login/register.jpg" alt="">
        <div class="login-form">
            <el-form ref="registerForm" :model="registerForm" :rules="registerRules" >
                <div class="login-form-title" style="text-align: center;">
                    <img src="../img/logo.png" style="width:139px;height:50px;" alt="" />
                </div>
                <div style="text-align: center;margin-top: -10px">
                <p id="username_err" style="display:none;color: red;font-size: small;">该用户已存在</p>
                </div>
                <el-form-item prop="username" style="margin-top: 10px">
                    <el-input v-model="registerForm.username" id="username" type="text" auto-complete="off" placeholder="账号" maxlength="20"
                              prefix-icon="iconfont icon-user" />
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="registerForm.password" type="password" placeholder="密码" prefix-icon="iconfont icon-lock" maxlength="20"
                             />
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="registerForm.password1" type="password" placeholder="确认密码" prefix-icon="iconfont icon-lock" maxlength="20"
                             />
                </el-form-item>
                <el-form-item style="width:100%;">
                    <el-button class="login-btn" size="medium" type="primary" style="width:100%;"
                               @click.native.prevent="handleRegister">
                        <span>注册</span>
                    </el-button>
                </el-form-item>
                <div style="text-align: center;">
                    <a href="../login_page/login.html" style="color: #8c939d;font-size: 14px;">已有账号？去登录...</a>
                </div>
            </el-form>
        </div>
    </div>
</div>

<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../plugins/axios/axios.min.js"></script>
<script src="../js/request.js"></script>
<script src="../js/validate.js"></script>
<script src="../js/login.js"></script>

<script>
    new Vue({
        el: '#login-app',
        data() {
            return {
                registerForm:{
                    username: '',
                    password: ''
                },
            }
        },
        computed: {
            registerRules() {
                const validateUsername = (rule, value, callback) => {
                    if (value.length < 1 ) {
                        callback(new Error('请输入用户名'))
                    } else {
                        callback()
                    }
                }
                return {
                    'username': [{ 'validator': validateUsername, 'trigger': 'blur' }],
                }
            }
        },
        created() {

        },
        methods: {
            async handleRegister() {
                this.$refs.registerForm.validate(async (valid) => {
                    if (valid) {
                        this.loading = true
                        let res = await registerApi(this.registerForm)
                        console.log(res)
                        if (res.code === 1) {

                            window.location.href = '/login_page/login.html'
                        } else {
                            this.$message.error(res.msg)
                            this.loading = false
                        }
                    }
                })
            }
        }
    })
</script>
</body>
</html>

<script>
    document.getElementById("username").onblur = function () {
        //1.获取用户名
        var username=this.value;
        //2.1创建核心对象
        var xhttp;
        if (window.XMLHttpRequest){
            xhttp=new XMLHttpRequest();
        }else{
            xhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        //2.2发送请求
        xhttp.open("Get","/user/byUsername/"+username+"/"+0)
        xhttp.send();
        //2.3获取响应
        xhttp.onreadystatechange =function (){
            if (this.readyState ==4 &&this.status==200){
                console.log(this.responseText)
                //用户存在，显示提示信息
                if (this.responseText ===''){
                    document.getElementById("username_err").style.display='none';
                }else
                    document.getElementById("username_err").style.display='';
            }
        };
    }

</script>
